#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LINE_LENGTH 256
#define FILENAME "data.csv"

// Function prototypes
void addRecord();
void deleteRecord();
void updateRecord();
void searchRecord();
void showRecord();

int main() {
    int choice;

    while (1) {
        printf("\nCSV File Operations:\n");
        printf("1. Add Record\n");
        printf("2. Delete Record\n");
        printf("3. Update Record\n");
        printf("4. Search Record\n");
        printf("5. Exit\n");
        printf("6. Show Record\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                addRecord();
                break;
            case 2:
                deleteRecord();
                break;
            case 3:
                updateRecord();
                break;
            case 4:
                searchRecord();
                break;
            case 5:
                printf("Exiting...\n");
                return 0;
            case 6:
                showRecord();
                break;
            default:
                printf("Invalid choice. Please try again.\n");
        }
    }

    return 0;
}

void addRecord() {
    FILE *file = fopen(FILENAME, "a");
    if (file == NULL) {
        perror("Error opening file");
        return;
    }

    int id;
    char name[50];
    int age;

    printf("Enter ID: ");
    scanf("%d", &id);
    printf("Enter Name: ");
    scanf("%s", name);
    printf("Enter Age: ");
    scanf("%d", &age);

    fprintf(file, "%d,%s,%d\n", id, name, age);
    fclose(file);
    printf("Record added successfully.\n");
}

void deleteRecord() {
    FILE *file = fopen(FILENAME, "r");
    if (file == NULL) {
        perror("Error opening file");
        return;
    }

    FILE *temp = fopen("temp.csv", "w");
    if (temp == NULL) {
        perror("Error opening temporary file");
        fclose(file);
        return;
    }

    int idToDelete;
    printf("Enter ID to delete: ");
    scanf("%d", &idToDelete);

    char line[MAX_LINE_LENGTH];
    int found = 0;

    while (fgets(line, MAX_LINE_LENGTH, file)) {
        int id;
        char name[50];
        int age;

        if (sscanf(line, "%d,%[^,],%d", &id, name, &age) == 3) {
            if (id != idToDelete) {
                fprintf(temp, "%s", line);
            } else {
                found = 1;
            }
        }
    }

    fclose(file);
    fclose(temp);

    if (found) {
        remove(FILENAME);
        rename("temp.csv", FILENAME);
        printf("Record deleted successfully.\n");
    } else {
        remove("temp.csv");
        printf("Record not found.\n");
    }
}

void updateRecord() {
    FILE *file = fopen(FILENAME, "r");
    if (file == NULL) {
        perror("Error opening file");
        return;
    }

    FILE *temp = fopen("temp.csv", "w");
    if (temp == NULL) {
        perror("Error opening temporary file");
        fclose(file);
        return;
    }

    int idToUpdate;
    printf("Enter ID to update: ");
    scanf("%d", &idToUpdate);

    char line[MAX_LINE_LENGTH];
    int found = 0;

    while (fgets(line, MAX_LINE_LENGTH, file)) {
        int id;
        char name[50];
        int age;

        if (sscanf(line, "%d,%[^,],%d", &id, name, &age) == 3) {
            if (id == idToUpdate) {
                printf("Enter new Name: ");
                scanf("%s", name);
                printf("Enter new Age: ");
                scanf("%d", &age);
                fprintf(temp, "%d,%s,%d\n", id, name, age);
                found = 1;
            } else {
                fprintf(temp, "%s", line);
            }
        }
    }

    fclose(file);
    fclose(temp);

    if (found) {
        remove(FILENAME);
        rename("temp.csv", FILENAME);
        printf("Record updated successfully.\n");
    } else {
        remove("temp.csv");
        printf("Record not found.\n");
    }
}

void searchRecord() {
    FILE *file = fopen(FILENAME, "r");
    if (file == NULL) {
        perror("Error opening file");
        return;
    }

    int idToSearch;
    printf("Enter ID to search: ");
    scanf("%d", &idToSearch);

    char line[MAX_LINE_LENGTH];
    int found = 0;

    while (fgets(line, MAX_LINE_LENGTH, file)) {
        int id;
        char name[50];
        int age;

        if (sscanf(line, "%d,%[^,],%d", &id, name, &age) == 3) {
            if (id == idToSearch) {
                printf("ID: %d, Name: %s, Age: %d\n", id, name, age);
                found = 1;
                break;
            }
        }
    }

    fclose(file);

    if (!found) {
        printf("Record not found.\n");
    }
}

void showRecord() {
    FILE *file = fopen(FILENAME, "r");
    if (file == NULL) {
        perror("Error opening file");
        return;
    }
    
    char line[MAX_LINE_LENGTH];
    printf("=================\n");
    printf("ID\tName\t\t\tAge\n");

    while (fgets(line, MAX_LINE_LENGTH, file)) {
        int id;
        char name[50];
        int age;
        if (sscanf(line, "%d,%[^,],%d", &id, name, &age) == 3) {
            // printf("%d\t%s\t\t%d\n", id, name, age);
            printf("%-4d\t%-20s\t%d\n", id, name, age);
        }
    }
    printf("=================\n");

    fclose(file);
}